import {
  teachers,
  teachers_pass,
  admins,
  admins_pass,
  donators,
  configs
} from "./mongo_non_student"

import {
  students,
} from "./mongo_student"

var err = {
  error_no: 2,
  error_msg: "内部错误，请联系杜思聪～"
}

var set_configs = (type, key, res, openid) => {

  configs.create({
    type: type,
    key: key,
    openId: openid
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "saved"
      })
    }
  })
}

var set_student_info = (req, res, openid) => {
  let {
    studentNo,
    classNo,
    name,
    userInfo,
    sex,
    school
  } = req.body

  students.create({
    studentNo: studentNo,
    classNo: classNo,
    name: name,
    userInfo: userInfo,
    sex: sex,
    school: school
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      set_configs("1", req.body.studentNo, res, openid)
    }
  })
}

var set_teacher_info = (req, res) => {
  let {
    name,
    teacherNo,
    userInfo,
    sex,
    school,
    password
  } = req.body

  teachers.create({
    name: name,
    teacherNo: teacherNo,
    userInfo: userInfo,
    sex: sex,
    school: school
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      teachers_pass.create({
        teacherNo: teacherNo,
        pass: password
      }, (error, doc) => {
        if (error) {
          res.send(err)
        } else {
          set_configs("2", req.body.teacherNo, res, "")
        }
      })
    }
  })
}

var set_admin_info = (req, res) => {
  admins.create({
    name: req.body.name,
    adminNo: req.body.adminNo,
    userInfo: req.body.userInfo,
    sex: req.body.sex,
    school: req.body.school
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      admins_pass.create({
        adminNo: req.body.adminNo,
        pass: req.body.password
      }, (error, doc) => {
        if (error) {
          res.send(err)
        } else {
          set_configs("3", req.body.adminNo, res, "")
        }
      })
    }
  })
}

var set_donator_info = (req, res, openid) => {
  donators.create({
    donatorNo: req.body.donatorNo,
    name: req.body.name,
    sex: req.body.sex,
    birthday: req.body.birthday,
    phone: req.body.phone,
    email: req.body.email,
    userInfo: req.body.userInfo,
    grad_school: req.body.grad_school,
    year: req.body.year
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      set_configs("4", req.body.donatorNo, res, openid)
    }
  })
}

var schools = require("./mongo_non_student").schools
var set_school = (req, res) => {
  var sch_doc = req.body.school
  schools.findOne({name: sch_doc}, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      if (doc != null) {
        res.send({
          error_no: 2,
          error_msg: "已添加～"
        })
      } else if (doc == null) {
        schools.create({
          name: sch_doc
        }, (error, doc) => {
          if (error) {
            res.send(err)
          } else {
            var doc_ret = {
              name: doc.name,
              id: doc._id.toString()
            }
            res.send({
              error_no: 0,
              error_msg: doc_ret
            })
          }
        })
      }

    }
  })

}

var setUserInfo = (req, res) => {
  var kind = req.body.kind
  if (kind == "5") {
    set_school(req, res)
  } else if (kind == "2") {
    set_teacher_info(req, res)
  } else if (kind == "3") {
    set_admin_info(req, res)
  } else {
    var getOpenId = require("./login/getOpenId").getOpenId
    getOpenId(req.body.code, (openid) => {
      switch (kind) {
        case '1':
          return set_student_info(req, res, openid)
          break;
        case '4':
          return set_donator_info(req, res, openid)
          break;
        default:
      }
    }, res)
  }
}

module.exports = {
  setUserInfo: setUserInfo
}
